import grace from "../../../utils/grace.js";
import {Category} from '../category/category-model.js';
import {Product} from 'product-model.js';

let category = new Category();
let product = new Product();

grace.page({
    /**
     * 页面的初始数据
     */
    data: {
        category_id: 0,
        keywords: '',
        banners: [],
        products: [],
        total: 0,
        page: 1,
        pagesize: 20
    },
    onLoad(e) {
        if(e.category_id != undefined){
            this.$data.category_id = e.category_id;
            category.getCategoryBanner({'category_id': e.category_id}, (data) => {
                if (data.code == 0) {
                    this.$data.banners = data.data;
                }
            })
        }
        if(e.keywords != undefined){
            this.$data.keywords = e.keywords;
        }

        this.getProducts();
    },
    getProducts() {
        // 查询当前分类的产品
        let params = {
            'page': this.$data.page,
            'pagesize': this.$data.pagesize
        }
        if(this.$data.category_id != 0){
            params['category_id'] = this.$data.category_id;
        }
        if(this.$data.keywords != ''){
            params['keywords'] = this.$data.keywords;
        }
        if(wx.getStorageSync('partner_id')){
            params['partner_id'] = wx.getStorageSync('partner_id')
        }
        product.getProductList(params, (data) => {
            if (data.code == 0) {
                this.$data.total = data.data.total;
                this.$data.page = this.$data.page + 1;
                this.$data.products = this.$data.products.concat(data.data.list);
            }
        })
    },
    onReachBottom() {
        if (this.$data.products.length < this.$data.total) {
            this.getProducts();
        }
    }
})